All files / api Database.ts

98.25% Statements 56/57
90% Branches 9/10
100% Functions 11/11
98.18% Lines 54/55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156                                12x 12x 12x 12x 12x 12x 12x 12x                 12x       12x                   47x 47x 1x           46x   46x     12x 14x                             12x 323x 323x   322x 2x     320x                   12x   5x 5x 5x 4x 4x   4x 4x 2x                     2x           12x 360x           12x 5x 5x 5x     12x 5x 5x 5x   12x   12x   46x     12x 22x 22x   22x 22x 22x 22x   12x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { fatal } from '../core/util/util';
import { parseRepoInfo } from '../core/util/libs/parser';
import { Path } from '../core/util/Path';
import { Reference } from './Reference';
import { Repo } from '../core/Repo';
import { RepoManager } from '../core/RepoManager';
import { validateArgCount } from '@firebase/util';
import { validateUrl } from '../core/util/validation';
import { FirebaseApp } from '@firebase/app-types';
import { FirebaseService } from '@firebase/app-types/private';
import { RepoInfo } from '../core/RepoInfo';
 
/**
 * Class representing a firebase database.
 * @implements {FirebaseService}
 */
export class Database implements FirebaseService {
  INTERNAL: DatabaseInternals;
  private root_: Reference;
 
  static readonly ServerValue = {
    TIMESTAMP: {
      '.sv': 'timestamp'
    }
  };
 
  /**
   * The constructor should not be called by users of our public API.
   * @param {!Repo} repo_
   */
  constructor(private repo_: Repo) {
    if (!(repo_ instanceof Repo)) {
      fatal(
        "Don't call new Database() directly - please use firebase.database()."
      );
    }
 
    /** @type {Reference} */
    this.root_ = new Reference(repo_, Path.Empty);
 
    this.INTERNAL = new DatabaseInternals(this);
  }
 
  get app(): FirebaseApp {
    return this.repo_.app;
  }
 
  /**
   * Returns a reference to the root or to the path specified in the provided
   * argument.
 
   * @param {string|Reference=} path The relative string path or an existing
   * Reference to a database location.
   * @throws If a Reference is provided, throws if it does not belong to the
   * same project.
   * @return {!Reference} Firebase reference.
   **/
  ref(path?: string): Reference;
  ref(path?: Reference): Reference;
  ref(path?: string | Reference): Reference {
    this.checkDeleted_('ref');
    validateArgCount('database.ref', 0, 1, arguments.length);
 
    if (path instanceof Reference) {
      return this.refFromURL(path.toString());
    }
 
    return path !== undefined ? this.root_.child(path) : this.root_;
  }
 
  /**
   * Returns a reference to the root or the path specified in url.
   * We throw a exception if the url is not in the same domain as the
   * current repo.
   * @param {string} url
   * @return {!Reference} Firebase reference.
   */
  refFromURL(url: string): Reference {
    /** @const {string} */
    const apiName = 'database.refFromURL';
    this.checkDeleted_(apiName);
    validateArgCount(apiName, 1, 1, arguments.length);
    const parsedURL = parseRepoInfo(url);
    validateUrl(apiName, 1, parsedURL);
 
    const repoInfo = parsedURL.repoInfo;
    if (repoInfo.host !== ((this.repo_ as any).repoInfo_ as RepoInfo).host) {
      fatal(
        apiName +
          ': Host name does not match the current database: ' +
          '(found ' +
          repoInfo.host +
          ' but expected ' +
          ((this.repo_ as any).repoInfo_ as RepoInfo).host +
          ')'
      );
    }
 
    return this.ref(parsedURL.path.toString());
  }
 
  /**
   * @param {string} apiName
   */
  private checkDeleted_(apiName: string) {
    Iif (this.repo_ === null) {
      fatal('Cannot call ' + apiName + ' on a deleted database.');
    }
  }
 
  // Make individual repo go offline.
  goOffline() {
    validateArgCount('database.goOffline', 0, 0, arguments.length);
    this.checkDeleted_('goOffline');
    this.repo_.interrupt();
  }
 
  goOnline() {
    validateArgCount('database.goOnline', 0, 0, arguments.length);
    this.checkDeleted_('goOnline');
    this.repo_.resume();
  }
}
 
export class DatabaseInternals {
  /** @param {!Database} database */
  constructor(public database: Database) {}
 
  /** @return {Promise<void>} */
  async delete(): Promise<void> {
    (this.database as any).checkDeleted_('delete');
    RepoManager.getInstance().deleteRepo((this.database as any).repo_ as Repo);
 
    (this.database as any).repo_ = null;
    (this.database as any).root_ = null;
    this.database.INTERNAL = null;
    this.database = null;
  }
}